package com.xingzhi.common.util;

import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by daniel.luo on 2018/5/23.
 */
public class DataUtil {

    public static Map<String, Object> generateMap(String[] keys, Object[] vals) {
        if (null == keys || null == vals || keys.length != vals.length) return null;
        Map<String, Object> map = new HashMap<String, Object>();
        for (int i = 0; i < keys.length; i++) {
            map.put(keys[i] + "", vals[i]);
        }
        return map;
    }

    public static Map<String, String> generateMap(String[] keys, String[] vals) {
        if (null == keys || null == vals || keys.length != vals.length) return null;
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < keys.length; i++) {
            map.put(keys[i] + "", vals[i]);
        }
        return map;
    }


    public static Map<String, String> generateMap(String... strs) {
        Map<String, String> map = new HashMap<String, String>();
        for (String str : strs) {
            if (str.contains(":")) {
                String[] ns = str.split(":");
                map.put(ns[0], ns[1]);
            }
        }
        return map;
    }


    /**
     * @param sourceAnswer  学生写的答案
     * @param correctAnswer 题库正确答案
     * @return
     */
    public static boolean compareCorrectAnswer(String sourceAnswer, String correctAnswer) {
        if (StringUtils.isEmpty(sourceAnswer) || StringUtils.isEmpty(correctAnswer)) return false;
        //1.(A,B,C,D)先替换, 变成(ABCD)
        sourceAnswer = sourceAnswer.replaceAll(",", "");
        correctAnswer = correctAnswer.replaceAll(",", "");
        //2.排序
        sourceAnswer = sort(sourceAnswer.toUpperCase());
        correctAnswer = sort(correctAnswer.toUpperCase());
        //3.进行对比
        if (sourceAnswer.equalsIgnoreCase(correctAnswer)) {
            return true;
        } else {
            return false;
        }
    }

    public static String compareGroupCorrectAnswer(String sourceAnswer, String correctAnswer) {
        if (StringUtils.isEmpty(sourceAnswer) || StringUtils.isEmpty(correctAnswer)) return "false";
        //1.切数组
        String[] source = sourceAnswer.split(",", -1);
        String[] correct = correctAnswer.split(",", -1);
        //2.对比
        String ret = "";
        for (int index = 0; index < source.length; index++) {
            if (source[index].toUpperCase().equals(correct[index].toUpperCase())) ret += "true";
            else ret += "false";
            if (index != source.length - 1) ret += ",";
        }
        return ret;
    }

    private static String sort(String str) {
        //把字符串转化成字符数组
        char[] chs = stringToCharArray(str);

        //对字符数组进行排序
        charSort(chs);

        //把字符数组转化成字符串
        String s = new String(chs);

        return s;
    }

    private static void charSort(char[] chs) {
        Arrays.sort(chs);
    }

    private static char[] stringToCharArray(String str) {
        return str.toCharArray();
    }

}




